<template>
  <div class="user-info">
    <el-row type="flex" justify="end">
      <el-tooltip content="打印个人基本信息">
        <router-link :to="`/employees/print/${userId}?type=personal`">
          <i class="el-icon-printer" />
        </router-link>
      </el-tooltip>
    </el-row>
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker v-model="userInfo.timeOfEntry" style="width: 300px" type="date" class="inputW"
              value-format="YYYY-MM-DD" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input style="width: 300px" v-model="userInfo.departmentName" class="inputW" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <UploadImg ref="uploadImg"></UploadImg>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
  import { getEmployeeInfoAPI, saveEmployeByIdAPI } from '@/api'
  import EmployeeEnum from '@/api/constant/employees'

  export default {
    data () {
      return {
        userId: this.$route.params.id,
        EmployeeEnum, // 员工枚举数据
        userInfo: {}
      }
    },
    created () {
      // 调用获取用户信息的 API
      this.loadUserInfoById()
    },
    methods: {
      async loadUserInfoById () {
        const res = await getEmployeeInfoAPI(this.userId).catch(err => err)
        this.userInfo = res
        if (res.staffPhoto && res.staffPhoto.trim()) {
          this.$refs.uploadImg.fileList = [{ url: res.staffPhoto, update: true }]
        }
      },

      async saveUser () {
        // 首先去读取图片组件中的数据
        const fileList = this.$refs.uploadImg.fileList
        if (fileList.length > 0) {
          console.log('大于0');
          if (fileList.some(item => !item.update)) {
            // 说明此时图片正在上传
            this.$message.warning('您当前还有图片没有上传完成！')
            return
          }
          this.userInfo = { ...this.userInfo, staffPhoto: fileList[0].update ? fileList[0].url : ' ' }
        } else {
          this.userInfo = { ...this.userInfo, staffPhoto: ' ' }
        }

        try {
          await saveEmployeByIdAPI(this.userInfo)
        } catch { }
        this.$message.success('保存成功')
        this.loadUserInfoById()
      }
    }
  }
</script>

<style scoped lang="scss">
  .user-info {
    padding-top: 20px;
  }
</style>